package com.scs.application.modules.base.aspect;

import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.dto.WebSocketMsgDTO;
import com.scs.application.core.socket.SpdWebSocketServerService;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.annotation.MarkParameter;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


/**
* @Description： 全局参数变化后
*/
@Aspect
@Component
@Slf4j
public class GlobalParametersAspect {
    @Autowired
    private SpdWebSocketServerService spdWebSocketServerService;

    @Pointcut("@annotation(com.scs.application.modules.base.annotation.MarkParameter)")
    public void pointcut() {
    }

    /**
     * 在切入点return内容之后切入内容（可以用来对处理返回值做一些加工处理）
     * @param joinPoint
     */
    @AfterReturning("pointcut()")
    public void doAfterReturning(JoinPoint joinPoint) {
        log.debug("GlobalParametersAspect.doAfterReturning");
        //目标方法实体
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        MarkParameter markParameter = method.getAnnotation(MarkParameter.class);  //获取方法上的注解
        String  type = markParameter.type();  //获取注解函数值
        String  msg = StringUtils.defaultString(markParameter.msg(),"参数默认变化提示信息");  //获取注解函数值
        WebSocketMsgDTO webSocketMsg = new WebSocketMsgDTO();
        webSocketMsg.setType(GlobalConsts.WebSocketMsgType.PARAM_CHANGE).setTypeSub(type).setMsg(msg);
        spdWebSocketServerService.sendTextAll(webSocketMsg);
    }

}
